import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getUserInfo } from '@/api/index'


export const userStore = defineStore('user', () => {
    //1.定义用户数据的管理状态
    const userInfo = ref({})
    //2.定义获取接口数据的action函数

    const setUserInfo = () => {
        getUserInfo().then(res => {
            console.log("setUserInfo")
            userInfo.value = res.data
        })

    }
    //3.以对象形式把action和state返回出去
    return { userInfo, setUserInfo }

}, {
    persist: true,
    storage: localStorage, // 存储位置修改为 sessionStorage
})

//单聊对象
export const toUserStore = defineStore('toUser', () => {
    //定义状态
    const toUserInfo = ref(null);

    //定义action
    const setToUserInfo = (data) => {
        console.log("设置第一个朋友", data)
        toUserInfo.value = data;
    }
    //将定义返回出去
    return { toUserInfo, setToUserInfo }

}, {
    persist: {
        enabled: true,
        storage: sessionStorage,
    }
})